<template>
    <div>
      <maintainDetail v-if="detailVisible" @changeDetailVisible="changeDetailVisible"
                      :effPlanDetail="effDetail">
      </maintainDetail>

      <addMaintainPlan v-else-if="addVisible" @addMaintain="addMaintain" :mainDevice="deviceList"
                       :factoryList="companyList" :planPositionList="positionList" :planClassifyList="classifyList"
                       @handlePlanOk="handlePlanOk" :editId="id">
      </addMaintainPlan>

      <div class="project zy-content" v-show="!detailVisible && !addVisible">
        <Tabs class="tabs-layout mb16" @on-click="tabsClick" v-model="status" :animated="false">
          <TabPane :label="$t('maintenance.ValidPlan')">
          </TabPane>
          <TabPane :label="$t('maintenance.InvalidPlan')">
          </TabPane>
        </Tabs>
        <efficacious ref="efficacious" v-if="status == 0" :planIds="ids" @addMaintain="addMaintain"
                     @changeDetailVisible="changeDetailVisible" :device="deviceList" :effPlanList="effList"
                     :effPlanTotal="effTotal" :permission="checkBtnPermission">
          <Button slot="btns" @click="addMaintain(true)" v-if="checkBtnPermission.dcmMaintainPlanAdd">{{ $t('maintenance.NewPlan')  }}</Button>
        </efficacious>

        <invalid  ref="invalid" v-else :planIds="ids" @addMaintain="addMaintain" @changeDetailVisible="changeDetailVisible"
                 :device="deviceList" :invalidPlanList="invalidList" :invalidPlanTotal="invalidTotal"
                 :permission="checkBtnPermission">
          <!--            <Button slot="btns" @click="addMaintain(true)" v-if="checkBtnPermission.dcmMaintainPlanAdd">{{ $t('maintenance.NewPlan')  }}</Button>-->
        </invalid>
      </div>
    </div>
</template>

<script>
import efficacious from './maintain/efficacious.vue'
import invalid from './maintain/invalid.vue'
import addMaintainPlan from './maintain/addMaintainPlan.vue'
import maintainDetail from './maintain/maintainDetail.vue'

export default {
    name: 'maintainPlanPage',
    components: {
        efficacious,
        invalid,
        addMaintainPlan,
        maintainDetail
    },
    data() {
        return {
            id: '',
            ids: [],
            status: 0,
            effTotal: 0,
            cycleList: [
                {name: "全部", key: ""},
                {name: '小时', key: '8'},
                {name: "班次", key: "7"},
                {name: '日', key: '1'},
                {name: '周', key: '2'},
                {name: '月', key: '3'},
                {name: '季度', key: '4'},
                {name: '半年', key: '5'},
                {name: '年', key: '6'},
            ],
            invalidTotal: 0,
            addVisible: false,
            detailVisible: false,
            effDetail: {},
            checkBtnPermission: {},
            deviceList: [],
            companyList: [],
            positionList: [],
            classifyList: [],
            effList: [],
            invalidList: [],

        }
    },
  activated(){

    // this.searchList()
  },
    created() {
        // this.getDeviceList()
        this.getCompanyList()
        // this.getPositionList()
        // this.getClassifyList()
        this.searchList()
    },
    mounted() {
        let itemList = JSON.parse(window.localStorage.getItem('USER_URI'))
        for (let item of itemList) {
            if (item.code == "dcmMaintainPlanAdd") {
                this.checkBtnPermission.dcmMaintainPlanAdd = true
            }
            if (item.code == "dcmMaintainPlanEdit") {
                this.checkBtnPermission.dcmMaintainPlanEdit = true
            }
            if (item.code == "dcmMaintainPlanQueryById") {
                this.checkBtnPermission.dcmMaintainPlanQueryById = true
            }
            if (item.code == "dcmMaintainPlanDelete") {
                this.checkBtnPermission.dcmMaintainPlanDelete = true
            }
            if (item.code == "dcmMaintainPlanList") {
                this.checkBtnPermission.dcmMaintainPlanList = true
            }
            if (item.code == "dcmMaintainPlanDeleteBatch") {
                this.checkBtnPermission.dcmMaintainPlanDeleteBatch = true
            }
            if (item.code == "dcmMaintainDisableRecordDisable") {
                this.checkBtnPermission.dcmMaintainDisableRecordDisable = true
            }
            if (item.code == "dcmMaintainDisableRecordBatchDisable") {
                this.checkBtnPermission.dcmMaintainDisableRecordBatchDisable = true
            }
            if (item.code == "dcmMaintainDisableRecordEnable") {
                this.checkBtnPermission.dcmMaintainDisableRecordEnable = true
            }
            if (item.code == "dcmMaintainDisableRecordBatchEnable") {
                this.checkBtnPermission.dcmMaintainDisableRecordBatchEnable = true
            }
            if (item.code == "dcmMaintainDisableRecordList") {
                this.checkBtnPermission.dcmMaintainDisableRecordList = true
            }
        }
    },
    methods: {
        /**************基础数据**********************/
        getDeviceList() {
            this.axios({
                method: 'get',
                url: '/dms/dmsCategory/getSelectList',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.deviceList = res.data.map(item => {
                        return {
                            ...item,
                            value: item.value.toString()
                        }
                    })
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
        //公司列表
        getCompanyList() {
            this.axios({
                method: 'get',
                url: '/basic/company/getCompanyListByUser',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.companyList = res.data
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
        getPositionList() {
            this.axios({
                method: 'get',
                url: '/dms/dcmMaintainPosition/getSelectList',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.positionList = res.data
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
        getClassifyList() {
            this.axios({
                method: 'get',
                url: '/dms/dcmMaintainClassify/getSelectList',
                params: {}
            }).then(res => {
                if (res.code === 200) {
                    this.classifyList = res.data
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
        tabsClick(name) {
            console.log(name)
            this.status = name
            this.ids = []
            this.searchList()
            // if (name === 0) {
            //     this.$refs.productConfigComponent.searchBtn();
            // } else  if (name === 1){
            //     console.log(this.$$refs);
            //     this.$refs.boxConfigComponent.searchBtn();
            // }else{
            //     this.$refs.shfitconfigComponent.searchBtn();
            // }
        },
        searchList() {
            let data = {
                status: this.status,
                pageNo: 1,
                pageSize: 10,
            };
            // for (let key in this.searchParams) {
            //   if (key === 'orgId' && this.searchParams['orgId']) {
            //     const arr = this.searchParams[key]
            //     const { length, last=length-1 } = arr
            //     this.$set(params, key, arr[last])
            //   }else {
            //     if (this.searchParams[key]) {
            //       this.$set(params, key, this.searchParams[key])
            //     }
            //   }
            //
            // }
            this.axios({
                method: 'POST',
                url: '/dms/dcmMaintainPlan/list',
                data
            }).then(res => {
                if (res.code === 200) {
                    if (this.status == 0) {
                        this.effList = res.data.list
                        this.effTotal = res.data.total
                    } else {
                        this.invalidList = res.data.list
                        this.invalidTotal = res.data.total
                    }
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
        handlePlanOk() {
            this.addVisible = false

            if (this.status==0)
            {
              this.$refs.efficacious.searchList()
            }else{
              this.$refs.invalid.searchList()
            }

        },
        addMaintain(flag, record) {
            let obj = record ? record : {}
            this.addVisible = flag
            if (obj.id) {
                this.id = record.id
            } else {
                this.id = ''
            }
        },
        changeDetailVisible(flag, id) {
            this.detailVisible = flag
            this.getEffDetail(id)
          if (!flag)
          {
            if (this.status==0)
            {
              this.$refs.efficacious.searchList()
            }else{
              this.$refs.invalid.searchList()
            }
          }
        },
        getEffDetail(id) {
            this.axios({
                method: 'get',
                url: '/dms/dcmMaintainPlan/queryById',
                params: {
                    id
                }
            }).then(res => {
                if (res.code === 200) {
                    this.effDetail = res.data
                    const obj = this.cycleList.find(val => val.key == this.effDetail.triggerCycle)
                    if (obj) {
                        this.effDetail.triggerCycleText = res.data.intervalRate + obj.name
                    }
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
    },
}
</script>

<style lang="scss">
.project {
    //background: #fff;

    .title {
        //display: flex;
        //justify-content: space-between;
        //font-size: 20px;
        //padding: 10px;
        //border-bottom: 1px solid #dddddd;

        .icon {
            display: flex;

            .creatBtnright {
                display: flex;
                align-items: center;
                font-size: 16px;

                i {
                    font-size: 36px;
                }
            }
        }
    }

    /*.tab {*/
    /*  height: calc(100% - 100px);*/
    /*  .ivu-tabs-bar {*/
    /*    padding-left: 30px;*/
    /*    .ivu-tabs-tabpane {*/
    /*      height: calc(100% - 100px);*/
    /*      overflow-y: scroll;*/
    /*    }*/
    /*  }*/
    /*  .ivu-tabs-tab {*/
    /*    font-size: 18px;*/
    /*  }*/
    /*}*/
    .ivu-tabs-tab {
        font-size: 18px;
    }
}

</style>
